<?php

class Pledges
{
	private $_db;
	private $_userSessionData;
	private $_utility;
	
	public function __construct()
	{
		// Start the DB objects
		$this->_db = Zend_Registry::get('db');
		$this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
   	}	
   	
   	/*********************************** MANAGE Pledges *************************************************/   	
	/* Function get_pledges($type,$start,$limit,$sidx,$sord,$filters)
	 * @Params: $type,$start,$limit,$sidx,$sord,$filters
	 * @Return: object - All records in the pledges table
	 * 
	 * Returns all the pledges in the pledges table
	 */
	public function get_pledges($type='all',$start=null,$limit=null,$sidx=null,$sord=null,$filters=null)
	{
		switch ($type)
		{
			case 'all':	
						$sql = "select * from pledges";
						if ($sidx && $sord)
						{
							$sql .= " ORDER BY $sidx $sord";
						}
						if ($start >= 0 && $limit)
						{
							$sql .= " LIMIT $start , $limit";
						}	
						$result = $this->_db->fetchAll($sql);
						break;
			case 'count': 
						$sql = "select count(*) as count from pledges"; 
						$result = $this->_db->fetchRow($sql);
						break;
		}			
		if ($result)
		{
			return $result;
		}
		return 0;
	}
	
	/* Function get_pledge_details($pledge_id)
	 * @Params: $pledge_id
	 * @Return: object 
	 * 
	 * Returns the pledge's details
	 */
	public function get_pledge_details($pledge_id)
	{
		$sql = "select * from pledges where pledge_id = '$pledge_id'";
		$result = $this->_db->fetchRow($sql);
		if ($result)
		{
			return $result;
		}
		return 0;
	}
	
	
	public function get_unverified_pledges()
	{
		$sql = "select * from pledges where verified = 0";
		$result = $this->_db->fetchAll($sql);
		if ($result)
		{
			return $result;
		}
		return 0;
	}
	
	/* Function get_next_pledge($pledge_id)
	 * @Params: $pledge_id
	 * @Return: object 
	 * 
	 * Returns the next available pledge_id
	 */
	public function get_next_pledge($pledge_id)
	{
		$sql = "SELECT pledge_id FROM pledges WHERE pledge_id = (SELECT MIN(pledge_id) FROM pledges WHERE pledge_id > $pledge_id)";
		$result = $this->_db->fetchRow($sql);
		if ($result)
		{
			return $result->pledge_id;
		}		
		// Return the first available pledge - hardcoded to pledge_id 13
		return 13;
	}
	
	/* Function get_latest_pledges
	 * Returns the 10 latest pledges 
	 */
	public function get_latest_pledges()
	{
			$sql = "select * from pledges order by created desc limit 10";
			$result = $this->_db->fetchAll($sql);
			if ($result)
			{
				return $result;
			}
			return 0;
	}
	
	public function get_pledge_counter($verified)
	{
			$sql = "select count(*) as count from pledges";
			$result = $this->_db->fetchRow($sql);
			if ($result)
			{
				return $result->count;
			}
			return 0;
	}
	
	public function get_distinct_countries()
	{
			$sql = "select count(distinct(country)) as count from pledges";			
			$result = $this->_db->fetchRow($sql);
			if ($result)
			{
				return $result->count;
			}
			return 0;
	}
	
	public function get_distinct_areas_of_expertise()
	{
			$sql = "select count(distinct(field)) as count from pledges";			
			$result = $this->_db->fetchRow($sql);
			if ($result)
			{
				return $result->count;
			}
			return 0;
	}
	
	
	
	/* Function create_pledge($full_name,$email,$phone_number,$country,$anonymous,$pledge,$field,$willingness,$city)
	 * @Params: $full_name,$email,$phone_number,$country,$anonymous,$pledge,$field,$willingness,$city
	 * @Return: new iso_id for success, 0 for failure
	 * 
	 * Creates a new pledge
	 */
	public function create_pledge($full_name,$email,$phone_number,$country,$anonymous,$pledge,$field,$willingness,$city)
	{
		$created = date ("Y-m-d H:i:s");
		$data = array('full_name'=>$full_name,'email'=>$email,'phone_number'=>$phone_number,'country'=>$country,'anonymous'=>$anonymous,'pledge'=>$pledge,'field'=>$field,'city'=>$city,'created'=>$created);
		try 
		{
			$this->_db->insert('pledges',$data);
			$pledge_id = $this->_db->lastInsertId();
			if ($this->create_pledge_willingness($pledge_id,$willingness))
			{
				return $pledge_id;
			}			
		} 
		catch (Exception $e) 
		{			
			return 0;
		}
	}
	
	/* Function create_pledge_willingness($pledge_id,$willingness)
	 * @Params: $pledge_id,$willingness
	 * @Return: new pledge_willingness_id for success, 0 for failure
	 */
	public function create_pledge_willingness($pledge_id,$willingness)
	{
		$data = array('pledge_id'=>$pledge_id);
		foreach ($willingness as $w)
		{
			$data[$w]=1;
		}
		try 
		{
			$this->_db->insert('pledge_willingness',$data);
			return $this->_db->lastInsertId();
		} 
		catch (Exception $e) 
		{			
			return 0;
		}
	}
	
	public function validate_pledge($pledge_id)
	{
		$data = array('verified'=>1);
		try 
		{
			$this->_db->update('pledges',$data,'pledge_id='.$pledge_id);
			return 1;
		} 
		catch (Exception $e) 
		{			
			return 0;
		}
	}
	
	
	public function plural($num) {
		if ($num != 1)
			return "s";
	}
	 
	public function getRelativeTime($date) {
		$diff = time() - strtotime($date);
		if ($diff<60)
			return $diff . " second" . $this->plural($diff) . " ago";
		$diff = round($diff/60);
		if ($diff<60)
			return $diff . " minute" . $this->plural($diff) . " ago";
		$diff = round($diff/60);
		if ($diff<24)
			return $diff . " hour" . $this->plural($diff) . " ago";
		$diff = round($diff/24);
		if ($diff<7)
			return $diff . " day" . $this->plural($diff) . " ago";
		$diff = round($diff/7);
		if ($diff<4)
			return $diff . " week" . $this->plural($diff) . " ago";
		return "on " . date("F j, Y", strtotime($date));
	}
			
}

?>